import click
import os
import platform
import sys
from collections import OrderedDict

import json
import pandas as pd
from pprint import pprint
from common.database import SessionLocal
from datetime import datetime, time

from app.callback.handlers.callback_pod_heal_handler import CallbackPodHealHandler
from app.callback.handlers.prometheus_metrics_handler import PrometheusMetricsHandler


from core.logger import logger
from common.welab_k8s import init_k8s


class CallBackClick(object):
    def __init__(self):
        self.session = SessionLocal()

    def __del__(self):
        self.session.close()


    def pod_self_healing(self, data: dict):
        """
        pod的故障自愈
        """
        init_k8s()
        callback_pod_heal_handler = CallbackPodHealHandler()
        result = callback_pod_heal_handler.post_heal_pod_abnormal(self.session, data)
        if not result:
            logger.error("post heal pod abnormal failed")
            return False

        logger.info("post heal pod abnormal success")
        return True


    def get_prometheus_metrics(self):
        """
        获取prometheus格式的metrics信息
        """
        prometheus_metrics_handler = PrometheusMetricsHandler()
        result = prometheus_metrics_handler.get_all_metrics(self.session)
        if not result:
            logger.error("get prometheus metrics failed")
            return False

        logger.info(result)
        logger.info("get prometheus metrics success")
        return True



def main():
    callback_click = CallBackClick()

    # #pod的故障自愈
    # data = {
    #     'annotations': {'key': 'value',
    #                     'message': 'pod:h5-wallet2-6bdbd5957c-6sznv有异常的容器',
    #                     'value': '0 '},
    #     'endsAt': '0001-01-01T00:00:00Z',
    #     'fingerprint': 'd906737e02729502',
    #     'generatorURL': 'http://prometheus-k8s-1:9090/graph?g0.expr=kube_pod_container_status_ready%7Bnamespace%3D%22prod%22%7D+%3D%3D+0+and+on%28container%2C+pod%29+kube_pod_container_status_restarts_total%7Bnamespace%3D%22prod%22%7D+%3E%3D+5+and+on%28pod%29+kube_pod_status_phase%7Bnamespace%3D%22prod%22%2Cphase%3D%22Running%22%7D+%3D%3D+1&g0.tab=1',
    #     'labels': {'alertname': 'ProdPodRunningFailed',
    #                'container': 'h5-wallet2',
    #                'i_callback': 'https://devops.welab-inc.com/ops/api/v1/monitor/callback/pod/heal/?token=tG9yadpT4E6P7wfC',
    #                'i_classes': 'K8s资源',
    #                'i_department': '运维',
    #                'token': "tG9yadpT4E6P7wfC",
    #                'i_level': 'nothing',
    #                'i_maintain_begin': '',
    #                'i_maintain_end': '',
    #                'i_maintain_repeat': '0',
    #                'i_notice_count': 1,
    #                'i_notice_teams_uuid': [],
    #                'i_target': '生产业务服务pod资源',
    #                'instance': '10.141.25.4:8443',
    #                'job': 'kube-state-metrics',
    #                'key': 'value',
    #                'namespace': 'prod',
    #                'pod': 'h5-wallet2-6bdbd5957c-6sznv',
    #                'prometheus': 'monitoring/k8s',
    #                'uid': '96b985f8-a6be-433f-941e-6903d7a56469'},
    #     'startsAt': '2022-09-26T06:35:46.979Z',
    #     'status': 'firing', #resolved
    #     'uuid': 3759
    # }
    # callback_click.pod_self_healing(data)
    
    callback_click.get_prometheus_metrics()

if __name__ == "__main__":
    main()
